<?php

class ZFKiller_Webservice {

	const TYPE_RETURN_JSON = 'JSON';
	const TYPE_RETURN_XML = 'XML';

	private $_content = array(
		'DATA' => null,
		'ERROR_CODE' => 0,
		'ERROR_MSG' => '',
	);

	/**
	 *
	 * @var Zend_Controller_Request_Abstract
	 */
	private $_request;

	/**
	 *
	 * @var Zend_Controller_Response_Abstract
	 */
	private $_response;

	/* private $_errorList = array(
	  0 => '',
	  400 => 'No results',
	  401 => 'Unauthorized',
	  404 => 'Not found',
	  999 => 'Unknown error',
	  ); */

	public function __construct() {
		$this->_request = Zend_Controller_Front::getInstance()->getRequest();
		$this->_response = Zend_Controller_Front::getInstance()->getResponse();
	}
	
	public function  getErrorCode() {
		return $this->_content['ERROR_CODE'];
	}

	public function setData($data) {
		$this->_content['DATA'] = $data;
	}

	public function setError($errorCode, $errorMsg) {
		$this->_content['ERROR_CODE'] = $errorCode;
		$this->_content['ERROR_MSG'] = $errorMsg;
	}

	public function sendResults() {
		
	}

	public function setHttpResponseCode($code) {
		$this->_response->setHttpResponseCode($code);
	}

	public function showResult($typeResult=self::TYPE_RETURN_XML, $content = null) {
		$content = (null === $content) ? $this->_content : $content;
		switch ($typeResult) {
			case self::TYPE_RETURN_JSON:
				$this->showResultAsJson($content);
				break;
		}

		$this->_response->sendResponse();
		exit();
	}

	public function showResultAsJson($content) {
		$this->_response->setHeader('content-type', 'application/json');
		$this->_response->setBody(json_encode($content));
	}

}